package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.JobEnterprise
import cn.exrick.xboot.base.mapper.JobEnterpriseMapper
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.IJobEnterpriseService
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 就职企业接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IJobEnterpriseServiceImpl : ServiceImpl<JobEnterpriseMapper?, JobEnterprise?>(), IJobEnterpriseService {

    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService

    override fun createOrUpdateJobEnterprise(jobEnterprise: JobEnterprise): Result<Any?>? {

        return try {

            jobEnterprise.enterpriseId = iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id)
            saveOrUpdate(jobEnterprise)
            ResultUtil.success("操作成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getDetailById(id: String?): Result<JobEnterprise>? {

        return ResultUtil.data(getById(id))
    }

    override fun removeJobEnterprise(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("移除成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getJobEnterpriseList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(JobEnterprise::class.java).eq(JobEnterprise::createBy, securityUtil.currUser.id)
                .orderByDesc(JobEnterprise::createTime)))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, this)

            ResultUtil.data(result, "获取数据成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }

}